package com.imooc.hystrix.controller;

import com.imooc.hystrix.bean.Friend;
import com.imooc.hystrix.service.MyService;
import com.imooc.hystrix.service.RequestCacheService;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class IndexController {

    @Autowired
    MyService service;

    @Autowired
    RequestCacheService requestCacheService;

    @GetMapping("test")
    public String test() {
        return service.error();
    }


    @GetMapping("timeOut")
    public String timeOut(
            @RequestParam("time")
                    Integer time
    ) {
        log.info("---->"+time);

        return service.timeOut(time);
    }

    @GetMapping("cache")
    public Friend cache(
            @RequestParam("name")
                    String name
    ) {

        @Cleanup HystrixRequestContext context = HystrixRequestContext.initializeContext();


        Friend friend = requestCacheService.requestCache(name);
        name+="!";
        friend = requestCacheService.requestCache(name);
        return friend;
    }

}
